package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.MessageSourceRelation;
import com.myzl.coal.domain.vo.SubscribeRelationVO;
import com.myzl.coal.mapper.MessageSourceRelationMapper;
import com.myzl.coal.service.IMessageSourceRelationService;
import com.myzl.common.core.domain.entity.SysUser;
import com.myzl.common.utils.DateUtils;
import com.myzl.common.utils.SecurityUtils;
import com.myzl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户订阅消息源Service业务层处理
 *
 * @author myzl
 * @date 2023-01-10
 */
@Service
public class MessageSourceRelationServiceImpl extends ServiceImpl<MessageSourceRelationMapper, MessageSourceRelation> implements IMessageSourceRelationService {
    @Autowired
    private MessageSourceRelationMapper messageSourceRelationMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户订阅消息源
     *
     * @param id 用户订阅消息源主键
     * @return 用户订阅消息源
     */
    @Override
    public MessageSourceRelation selectMessageSourceRelationById(Long id) {
        return messageSourceRelationMapper.selectMessageSourceRelationById(id);
    }

    /**
     * 查询用户订阅消息源列表
     *
     * @param messageSourceRelation 用户订阅消息源
     * @return 用户订阅消息源
     */
    @Override
    public List<MessageSourceRelation> selectMessageSourceRelationList(MessageSourceRelation messageSourceRelation) {
        return messageSourceRelationMapper.selectMessageSourceRelationList(messageSourceRelation);
    }

    /**
     * 新增用户订阅消息源
     *
     * @param messageSourceRelation 用户订阅消息源
     * @return 结果
     */
    @Override
    public int insertMessageSourceRelation(MessageSourceRelation messageSourceRelation) {
        messageSourceRelation.setUserId(SecurityUtils.getUserId());
        return messageSourceRelationMapper.insertMessageSourceRelation(messageSourceRelation);
    }

    /**
     * 修改用户订阅消息源
     *
     * @param messageSourceRelation 用户订阅消息源
     * @return 结果
     */
    @Override
    public int updateMessageSourceRelation(MessageSourceRelation messageSourceRelation) {
        messageSourceRelation.setUpdateTime(DateUtils.getNowDate());
        return messageSourceRelationMapper.updateMessageSourceRelation(messageSourceRelation);
    }

    /**
     * 批量删除用户订阅消息源
     *
     * @param ids 需要删除的用户订阅消息源主键
     * @return 结果
     */
    @Override
    public int deleteMessageSourceRelationByIds(Long[] ids) {
        return messageSourceRelationMapper.deleteMessageSourceRelationByIds(ids);
    }

    /**
     * 删除用户订阅消息源信息
     *
     * @param id 用户订阅消息源主键
     * @return 结果
     */
    @Override
    public int deleteMessageSourceRelationById(Long id) {
        return messageSourceRelationMapper.deleteMessageSourceRelationById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByMessageSourceId(MessageSourceRelation messageSourceRelation) {
        List<MessageSourceRelation> messageSourceRelations = messageSourceRelationMapper.selectMessageSourceRelationList(messageSourceRelation);
        if (CollectionUtils.isEmpty(messageSourceRelations)) {
            return 0;
        }
        List<Long> ids = messageSourceRelations.stream().map(MessageSourceRelation::getId).collect(Collectors.toList());
        int i = messageSourceRelationMapper.deleteBatchIds(ids);
        return i;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean subscribe(Long messageSourceId, List<Long> userIds) {
        Map<String, Object> paramsMap = new HashMap<>(16);
        paramsMap.put("message_source_id", messageSourceId);
        messageSourceRelationMapper.deleteByMap(paramsMap);

        List<MessageSourceRelation> messageSourceRelations = userIds.stream().map(e -> {
            MessageSourceRelation messageSourceRelation = new MessageSourceRelation();
            messageSourceRelation.setUserId(e);
            messageSourceRelation.setMessageSourceId(messageSourceId);
            return messageSourceRelation;

        }).collect(Collectors.toList());
        this.saveBatch(messageSourceRelations);

        return true;
    }

    @Override
    public SubscribeRelationVO subscribeRelation(Long messageSourceId) {
        MessageSourceRelation messageSourceRelation = new MessageSourceRelation();
        messageSourceRelation.setMessageSourceId(messageSourceId);
        List<MessageSourceRelation> messageSourceRelations = messageSourceRelationMapper.selectMessageSourceRelationList(messageSourceRelation);

        List<SysUser> sysUsers = sysUserService.selectUserList(new SysUser());
        List<Long> userIds = messageSourceRelations.stream().map(MessageSourceRelation::getUserId).collect(Collectors.toList());
        SubscribeRelationVO subscribeRelationVO = new SubscribeRelationVO();
        subscribeRelationVO.setUserIds(userIds);
        subscribeRelationVO.setSysUsers(sysUsers);
        return subscribeRelationVO;

    }
}
